<?php namespace App\Http\Controllers\Api;

use App\Article;
use App\Exceptions\NotFoundException;
use Illuminate\Support\Facades\Input;

class ArticleController extends ApiController
{
    public function buildArticle(){
        $id      = Input::get('id');
        $title   = Input::get('title');
        $imgId   = Input::get('img_id');
        $content = Input::get('content');

        $articleModel = Article::find($id);
        if(!$articleModel){
            $articleModel = new Article();
        }

        $articleModel->title   = !is_null($title) ? $title : $articleModel->title;
        $articleModel->img_id  = !is_null($imgId) ? $imgId : $articleModel->img_id;
        $articleModel->content = !is_null($content) ? $content : $articleModel->content;
        $articleModel->save();

        return $this->buildResponse(trans('提交成功'),$articleModel);
    }

    public function delArticle(){
        $id      = Input::get('id');

        /** @var Article $articleModel */
        $articleModel = Article::find($id);

        if(!$articleModel){
            throw new NotFoundException(NotFoundException::DataNotFound);
        }

        $articleModel->delete();

        return $this->buildResponse(trans('删除成功'));
    }
}